#include "editadditionalabilitiesform.h"
#include "ui_editadditionalabilitiesform.h"
#include <QDebug>
#include <QMenu>
#include <QListWidget>
#include <QListView>

EditAdditionalAbilitiesForm::EditAdditionalAbilitiesForm(QWidget *parent, AdditionalAbility * selectedAbility) :
    QDialog(parent),
    ui(new Ui::EditAdditionalAbilitiesForm)
{
    ui->setupUi(this);

    connect(ui->buttonBox, SIGNAL(accepted()), this, SLOT(SaveChanges()));

    if (selectedAbility == NULL)
        m_AdditionalAbility = new AdditionalAbility();
    else
    {
        m_AdditionalAbility = selectedAbility;
        ui->PriceSelectWidget->txtPrice->setText(m_AdditionalAbility->getPrice());
        if (m_AdditionalAbility->getPrice().indexOf(QString("<").append(WORD_SACRIFISE).append("/>")) != -1)
            ui->PriceSelectWidget->chkSacrifise->setChecked(true);
        if (m_AdditionalAbility->getPrice().indexOf(QString("<").append(WORD_TAP).append("/>")) != -1)
            ui->PriceSelectWidget->chkTap->setChecked(true);
        if (m_AdditionalAbility->getPrice().indexOf(QString("<").append(WORD_ONENTER).append("/>")) != -1)
            ui->PriceSelectWidget->chkOnEnter->setChecked(true);

        ui->txtCustomAbilities->setText(m_AdditionalAbility->getAbility());
    }
    CreateMenus();
}

EditAdditionalAbilitiesForm::~EditAdditionalAbilitiesForm()
{
    delete ui;
}

AdditionalAbility * EditAdditionalAbilitiesForm::getAdditionalAbility()
{
    return m_AdditionalAbility;
}

QString EditAdditionalAbilitiesForm::getAdditionalAbilityText()
{
    return m_AdditionalAbility->toText();
}


void EditAdditionalAbilitiesForm::btnWhiteClicked()
{
    appendColorToPriceLine(WORD_COLOR_WHITE);
}

void EditAdditionalAbilitiesForm::btnBlackClicked()
{
    appendColorToPriceLine("Black");
}

void EditAdditionalAbilitiesForm::btnBlueClicked()
{
    appendColorToPriceLine("Blue");
}

void EditAdditionalAbilitiesForm::btnRedClicked()
{
    appendColorToPriceLine("Red");
}

void EditAdditionalAbilitiesForm::btnGreenClicked()
{
        appendColorToPriceLine("Green");
}

void EditAdditionalAbilitiesForm::btnXClicked()
{
    appendColorToPriceLine("X");
}

void EditAdditionalAbilitiesForm::btnNoneClicked()
{
    appendColorToPriceLine("None");
}

void EditAdditionalAbilitiesForm::chkTapClicked(bool enabledChk)
{
    enableProperty("Tap", enabledChk);
}

void EditAdditionalAbilitiesForm::chkSacrifiseClicked(bool enabledChk)
{
    enableProperty("Sacrifise", enabledChk);
}

void EditAdditionalAbilitiesForm::SaveChanges()
{
    m_AdditionalAbility->Init(ui->PriceSelectWidget->txtPrice->text(), ui->txtCustomAbilities->toPlainText());
}

void EditAdditionalAbilitiesForm::CreateMenus()
{
    QMenu *menuActions = new QMenu(this);
    QMenu *subMenuLook = menuActions->addMenu("Look");
    subMenuLook->addAction("LookHand");
    subMenuLook->addAction("LookPool");
    subMenuLook->addAction("LookGrave");
    subMenuLook->addAction("LookPermanent");

    QMenu *subMenuChoose = menuActions->addMenu("Choose");
    subMenuChoose->addAction("ChooseOne");
    subMenuChoose->addAction("ChooseX");
    subMenuChoose->addAction("ChooseAll");

    QMenu *subMenuTargets = menuActions->addMenu("Targets");
    subMenuTargets->addAction("TargetCard");
    subMenuTargets->addAction("TargetSelf");
    subMenuTargets->addAction("TargetSelfPlayer");
    subMenuTargets->addAction("TargetTarget");
    subMenuTargets->addAction("TargetType( )");
    subMenuTargets->addAction("TargetColor( )");
    subMenuTargets->addAction("TargetEnchanted");
    subMenuTargets->addAction("TargetOpponent");


    menuActions->addSeparator();

    QMenu *subMenuActions = menuActions->addMenu("Actions");
    subMenuActions->addAction("ActionToGrave");
    subMenuActions->addAction("ActionFromGame");
    subMenuActions->addAction("ActionToHand");
    subMenuActions->addSeparator();
    subMenuActions->addAction("ActionUntap");
    subMenuActions->addAction("ActionControl");
    subMenuActions->addAction("ActionCounterSpell");
    subMenuActions->addSeparator();
    subMenuActions->addAction("ActionCantPlayInstants");
    subMenuActions->addAction("ActionCantPlaySorceries");
    subMenuActions->addAction("ActionCantPlayNonmanaAbilities");
    subMenuActions->addSeparator();
    subMenuActions->addAction("ActionDrawCard");


    QMenu *menuAddSome = new QMenu(this);
    menuAddSome->addAction("LifeDamage( , )");

    QMenu *subMenuMana = menuAddSome->addMenu("Mana");
    foreach (QString color, WORD_VECTORS::GET_ALL_COLORS())
        subMenuMana->addAction("Mana" + color);
    subMenuMana->addSeparator();
    subMenuMana->addAction("ManaAny");

    QMenu *menuAlternativeCost = new QMenu(this);
    menuAlternativeCost->addAction("AlternativeCost()");

    connect(menuActions, SIGNAL(triggered(QAction*)), this, SLOT(ActionsMenuTriggered(QAction*)));
    connect(menuAddSome, SIGNAL(triggered(QAction*)), this, SLOT(ActionsMenuTriggered(QAction*)));
    connect(menuAlternativeCost, SIGNAL(triggered(QAction*)), this, SLOT(ActionsMenuTriggered(QAction*)));
    ui->btnActions->setMenu(menuActions);
    ui->btnAddSome->setMenu(menuAddSome);
    ui->btnAlternativeCost->setMenu(menuAlternativeCost);
}

void EditAdditionalAbilitiesForm::ActionsMenuTriggered(QAction* action)
{
    if (action->text().compare("TargetTarget") == 0)
    {
        QDialog *dialog = new QDialog(this);

        QListWidget *wgt = new QListWidget(this);
        QVBoxLayout *layout = new QVBoxLayout();
        layout->addWidget(wgt);
        dialog->setLayout(layout);

        wgt->setSelectionMode(QAbstractItemView::MultiSelection);

        foreach(QString cardType, WORD_VECTORS::GET_ALL_TYPES())
        {
            QListWidgetItem *item = new QListWidgetItem(cardType);
            wgt->addItem(item);
        }
        dialog->exec();

        action->setText(action->text().append("("));
        foreach (QListWidgetItem * item, wgt->selectedItems())
        {
            action->setText(action->text().append(item->text().append(" | ")));
        }
        action->setText(action->text().append(")"));
    }
    ui->txtCustomAbilities->setText(ui->txtCustomAbilities->toPlainText() +
                                    " " + action->text());
}

void EditAdditionalAbilitiesForm::appendColorToPriceLine(QString addedColor)
{
    QString curText = ui->PriceSelectWidget->txtPrice->text();

    QDomDocument *doc = new QDomDocument();
    doc->setContent(curText);
    QDomNodeList nodes = doc->elementsByTagName(WORD_PRICE);

    QDomElement rootElement;
    if (nodes.isEmpty())
    {
        rootElement= doc->createElement(WORD_PRICE);
        doc->appendChild(rootElement);
    }
    else
        rootElement = doc->documentElement();

    nodes = doc->elementsByTagName(addedColor);
    if (nodes.isEmpty())
    {
        QDomElement el = doc->createElement(addedColor);
        el.setAttribute("Count", 1);
        rootElement.appendChild(el);
        ui->PriceSelectWidget->txtPrice->setText(doc->toString(4));
        return;
    }
    QDomNode node = nodes.item(0);
    QDomElement el = node.toElement();
    el.setAttribute("Count", el.attribute("Count", 0).toInt() + 1);

    ui->PriceSelectWidget->txtPrice->setText(doc->toString(4));
}

void EditAdditionalAbilitiesForm::enableProperty(QString enabledProperty, bool enabledChk)
{
    QString curText = ui->PriceSelectWidget->txtPrice->text();

    QDomDocument *doc = new QDomDocument();
    doc->setContent(curText);
    QDomNodeList nodes = doc->elementsByTagName(WORD_PRICE);

    QDomElement rootElement;
    if (nodes.isEmpty())
    {
        rootElement= doc->createElement(WORD_PRICE);
        doc->appendChild(rootElement);
    }
    else
        rootElement = doc->documentElement();

    if (enabledChk)
    {
        QDomElement el = doc->createElement(enabledProperty);
        rootElement.appendChild(el);
    }
    else
        rootElement.removeChild(doc->elementsByTagName(enabledProperty).item(0));
    ui->PriceSelectWidget->txtPrice->setText(doc->toString(4));
}
